package com.conferenceroom.config;

import com.conferenceroom.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(loginInterceptor)
//                .excludePathPatterns("/login/**",
//                        "/course",
//                        "/course/title",
//                        "/comments/**",
//                        "/categorys/**");
    }

    /**
     * 配置跨域访问
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //本项目中那些资源是允许跨域访问的
                .allowedOriginPatterns("*")//允许跨域访问的源  www.baidu.com 在spring 5.3之后使用allowedOriginPatterns替代allowedOrigins
//                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*") //允许跨域访问的请求头
                .allowCredentials(true); //是否允许携带cookie
    }
}
